<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <style>
        html, body {
            background-color: #f8f8f8;
        }
        .main {
        	background-color: #fff;
        }
        .main ul li {
        	height: 50px;
        	line-height: 50px;
        	border-bottom: 1px solid #e8e8e8;
        }
        .main ul li:last-child {
        	border-bottom: none;
        }
        .main ul li input {
        	width: 100%;
        	height: 100%;
        	color: #999;
            font-size: 14px;
        	padding: 0 15px;
        	box-sizing: border-box;
        }
        .main ul li input::-webkit-input-placeholder {
        	color: #999;
        }
    </style>
</head>
<body>
    <div class="main">
    	<ul>
    		<li>
    			<input class="password" type="password" placeholder="请输入原密码">
    		</li>
    		<li>
    			<input class="new_password" type="password" placeholder="请输入新密码">
    		</li>
    		<li>
    			<input class="re_new_password" type="password" placeholder="再次输入密码">
    		</li>
    	</ul>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/zepto.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript">
    apiready = function(){
        api.addEventListener({
            name: 'changePasswordEvent'
        }, function(ret, err) {
            if (ret) {
                var password = $('.password').val();
                var new_password = $('.new_password').val();
                var re_new_password = $('.re_new_password').val();
                if (!password) {
                    api.toast({
                        msg: '请输入密码',
                        location : "middle"
                    });
                    return;
                }
                if (!new_password) {
                    api.toast({
                        msg: '请输入新密码',
                        location : "middle"
                    });
                    return;
                }
                if (new_password.length<6 || new_password.length > 18) {
                    api.toast({
                        msg: '新密码6~18位之间',
                        location : "middle"
                    });
                    return;
                }
                if (new_password != re_new_password) {
                    api.toast({
                        msg: '两次密码不一致',
                        location : "middle"
                    });
                    return;
                }
                var shop = $api.getStorage('shop_data');
                api.ajax({
                    url: rootPath+'/api/seller/change_password',
                    method: 'post',
                    data: {
                        values: { 
                            shop_id: shop.id,
                            token:shop.token,
                            password:password,
                            new_password:new_password
                        },
                    }
                }, function(ret, err) {
                    if (ret) {
                        if(ret.status == 200) {
                            $api.setStorage('shop_data',null);
                            api.toast({
                                msg: '修改成功',
                                location : 'middle'
                            });
                            api.sendEvent({
                                name: 'logoutEvent',
                                extra: {
                                }
                            });
                            setTimeout(function(){
                                api.closeWin();
                            },1000)
                        }else {
                            api.toast({
                                msg: ret.msg,
                                location : 'middle'
                            });
                        }
                    } else {
                        alert(JSON.stringify(err.msg));
                    }
                });
            } 
        });
    };
</script>
</html>